
const cache = {};

export const setDict = function(key, children, dict) {
	cache[buildKey(key, children)] = JSON.stringify(dict);
}

export const getDict = function(key, children) {
	var result = null; 
	if(children) {
		result = cache[buildKey(key, children)];
	} else {
		result = cache[buildKey(key, true)] || cache[buildKey(key, false)];
	}
	return result ? JSON.parse(result) : null;
}

export const getDicts = function(values, children) {
	var result = {};
	Object.keys(values).map((key) => {
		var dict = getDict(values[key], children);
		if(dict) {
			delete values[key];
			result[key] = dict;
		}
	});
	return result;
}

export const setDicts = function(values, children, dicts) {
	Object.keys(dicts).map((key) => {
		var dict = dicts[key];
		if(dict) {
			setDict(values[key], children, dict);
		}
	});
}

function buildKey(key, children) {
	if(Array.isArray(key)) {
		return `${key.join()}-${children}`;
	}
	return key;
}

export default {
	setDict,
	getDict,
	getDicts,
	setDicts
}